# 创建字典
d={10:'cat',20:'dog',30:'tiger', 20:'lion'}
print(d) # key相同时，后面的key-value会覆盖前面的key-value

# zip() 函数创建字典
lst1 = [10, 20, 30]
lst2 = ['cat', 'dog', 'tiger', 'lion', 'elephant']
zipobj = zip(lst1, lst2)
print(zipobj) # zip 对象
# print(list(zipobj)) # 转换为列表
print(dict(zipobj)) # 转换为字典

# 使用函数创建字典
d=dict(a=10, b=20, c=30)
print(d)

# 使用元组创建字典
t=(10,20,30)
print({t:10})

d={'hello':10, 'world':20, 'python':30}
print(d['hello']) # 10
print(d.get('hello')) # 10

print(d.get('java')) # None
print(d.get('java', 'java is not in the dictionary')) #

# 向字典中添加元素
d['java'] = 40

# 字典的遍历
for key in d:
    print(key, d[key])

for item in d.items():
    print(item)

for key,value in d.items():
    print(key, value)

for key in d.keys():
    print('key=',key)

for value in d.values():
    print('value=',value)

print("===============================")
print("                               ")
print("                               ")


import random
d={item:random.randint(1,100) for item in range(1,11)}
print(d)

lst1=[10,20,30,40,50]
lst2=['a','b','c','d','e']
d={key:value for key,value in zip(lst1,lst2)}
print(d)

